package com.simple.piger.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 公开工具类
 * @Author  donghj
 * @Date    2017/11/17 10:18
 */
public class PublicUtil {

	/**
	 * 递归（返回树形结构）
	 * 	===================================================================
	 * 	入参：
	 *		jsonArray			【jsonArray数组】
	 *		parent_id			【一般起始pid都是0】
	 *		id_col_name			【主键字段名】
	 *		pid_col_name		【父节点主键字段名】
	 * 	===================================================================
	 * @Author  donghj
	 * @Date    2017/11/21 19:53
	 */
	public static JSONArray recursion(JSONArray jsonArray, int parent_id, String id_col_name, String pid_col_name) {
		JSONArray child = new JSONArray();
		for (Object object : jsonArray) {
			JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
			int id = jsonObject.getInteger(id_col_name);
			int pid = jsonObject.getInteger(pid_col_name);
			if (parent_id == pid) {
				JSONArray children = recursion(jsonArray, id, id_col_name, pid_col_name);
				jsonObject.put("children", children);
				child.add(jsonObject);
			}
		}
		return child;
	}

	/**
	 * HashMap转JSONObject
	 * @Author  donghj
	 * @Date    2017/11/30 9:35
	 */
	public static JSONObject getJSONObject(HashMap hashMap) {
		return JSONObject.parseObject(JSON.toJSONString(hashMap));
	}

	/**
	 * 获取查询起始索引
	 * @Author  dianmy
	 * @Date    2017/12/13 14:59
	 */
	public static int getPageStart(Integer current ,Integer size) {
        if (null == current) {
        	current = 1;
        }
        if (null == size) {
        	size = 10;
        }
        return (current - 1) * size;
    }

	/**
	 * 将以逗号分隔的字符串转换成字符串数组
	 * @Author  donghj
	 * @Date    2017/12/13 14:55
	 */
	public static String[] splitStringList(String string) {
		String[] rtn_string = new String[]{};
		if (!StringUtils.isNotBlank(string)) {
			return rtn_string;
		}
		rtn_string = StringUtils.split(string, ",");
		return rtn_string;
	}

	/**
	 * 手机号尾部四位掩码（和七鱼客服脱敏一致）
	 * @Author  donghj
	 * @Date    2017/12/19 17:59
	 */
	public static String maskMobile(String mobile) {
		mobile = mobile.replace(mobile.substring(7, 11), "****");
		return mobile;
	}

	/**
	 * 返回一个日期加字符串时间（H:M:S）的日期
	 * @Author  donghj
	 * @Date    2018/2/9 15:56
	 */
	public static Date getDateAfterTime(Date date, String time) {
		Date new_date = null;
		if (date != null && StringUtils.isNotBlank(time)) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			String[] timeArr = StringUtils.split(time, ":");
			int hours = Integer.parseInt(timeArr[0]);
			int minutes = Integer.parseInt(timeArr[1]);
			int seconds = Integer.parseInt(timeArr[2]);
			calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR)+hours);
			calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)+minutes);
			calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND)+seconds);
			new_date = calendar.getTime();
		}
		return new_date;
	}

	/**
	 * 生成随机数
	 * @Author  dianmy
	 * @Date    2018/3/9 15:52
	 */
	public static String getRandom(Integer size){
		Random random = new Random();
		String result = "";
		for (int i=0;i<size;i++) {
			result += random.nextInt(10);
		}
		return result;
	}

	/**
	 * 获取SessionToken
	 * @Author  donghj
	 * @Date    2018/4/18 19:42
	 */
	public static String getSessionToken(){
		return UUID.randomUUID().toString().replace("-", "") + RandomStringUtils.randomAlphanumeric(8);
	}
}